package com.wx.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.wx.interceptor.AuthInterceptor;
import com.wx.model.User;
import com.wx.service.UserService;
import com.wx.validator.LoginValidator;

import java.util.List;

/**
 * Created by myd on 2016/12/31.
 */
public class UserController extends Controller {

    private UserService userService = new UserService();

    public void index(){}

    @Before(LoginValidator.class)
    public void login(){
        String username = getPara("username");
        String password = getPara("password");
//        boolean loginCheck = userService.login(username,password);
//        if(loginCheck){
//            renderJson("10000");
//            getSession().setAttribute("flag",true);
//        }
//
//        else
//            renderJson("10001");
        List<Record> list= userService.find(username,password);
        if (list.size()>0)
        {
            list.get(0);
            renderJson(list.get(0));
        }
        else {
            renderJson(list);
        }
    }

    public void register(){
        String username = getPara("username");
        String password = getPara("password");
        boolean result = userService.add(username,password);
        if(result)
            renderJson("10010");
        else
            renderJson("10011");
    }

    @Before(AuthInterceptor.class)
    public void show(){
        renderJsp("user.jsp");
    }

    public void image(){
        try{
            getFile(getPara("img"),"UTF-8");
            renderJson("20010");
        } catch (Exception e){
            renderJson("20012");
        }
    }
}
